package com.example.dynamicschedule.task;

import com.example.dynamicschedule.bean.*;
import com.example.dynamicschedule.config.ScanningCleaningConfig;
import com.example.dynamicschedule.dao.CkCreditLogMapper;
import com.example.dynamicschedule.dao.CkCreditStopNoticePersonMapper;
import com.example.dynamicschedule.service.CkFeedBackConfigService;
import com.example.dynamicschedule.service.CkFeedBackService;
import com.example.dynamicschedule.service.CustomerService;
import com.example.dynamicschedule.service.IntelligenceCallService;
import com.example.dynamicschedule.utils.DateUtils;
import com.example.dynamicschedule.utils.FileDownloadUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class CreditDownLoadFileTask {
    static Logger log = Logger.getLogger(CreditDownLoadFileTask.class);
    @Autowired
    private ScanningCleaningConfig scanningCleaningConfig;
    @Autowired
    private DateUtils dateUtils;
    @Autowired
    private FileDownloadUtil fileDownloadUtil;
    @Autowired
    private CkFeedBackConfigService ckFeedBackConfigService;
    @Autowired
    private CkCreditLogMapper ckCreditLogMapper;
    @Autowired
    private CkFeedBackService feedBackService;
    @Autowired
    private CkCreditStopNoticePersonMapper ckCreditStopNoticePersonMapper;
    @Autowired
    private IntelligenceCallService intelligenceCallService;
    @Autowired
    private CustomerService customerService;

    /**
     * 三方下载反馈信息
     */
    @Transactional
    public void CreditDownLoadFileTask() throws Exception{
        log.info("信贷下载任务-------------------------开始");
        log.info("信贷下载任务---------智能客服下载oss服务上的json文件开始----------------开始");
        //失联停催反馈码
        Map<String, CkFeedbackConfigTable> feedBackConfigMap = ckFeedBackConfigService.selectList();
        //未拨打
        String noDia = feedBackService.selectByChinese("未拨打");
        //未接听
        String noCall = feedBackService.selectByChinese("未接听");
        //小与4 (抛出委外数据)
        List<CkCreditLog> ckCreditLogs = ckCreditLogMapper.selectByNowDayAndCollectionTypeAndState(4);
        //获取失联数据
        List<CkCreditStopNoticePerson> stopCkCreditStopNoticePeoples = ckCreditStopNoticePersonMapper.selectByStatus(1);
        stopCkCreditStopNoticePeoples.addAll(ckCreditStopNoticePersonMapper.selectByStatus(2));

        String prefixDownJsonPath = scanningCleaningConfig.getCreditPrefixDownJsonPath().replace("yyyyMMdd", dateUtils.getNowStr());
        String intelligenceDownloadJsonPath = scanningCleaningConfig.getCreditIntelligenceDownloadJsonPath();
        String localPath = intelligenceDownloadJsonPath + prefixDownJsonPath;
        File file = new File(intelligenceDownloadJsonPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileDownloadUtil.downLoad(scanningCleaningConfig.getPackageName() + "collection_credit_card_intelligence/" + prefixDownJsonPath, localPath);
        File localPathFile = new File(localPath);
        if (localPathFile.exists()) {
            intelligenceCallService.creditIntelligenceCall(localPath, feedBackConfigMap,
                    ckCreditLogs, noDia, stopCkCreditStopNoticePeoples,"智能外呼",noCall);
        } else {
            log.info("--------该地址:" + localPath + ",无文件(或oss上无文件)---------");
        }
        log.info("信贷下载任务---------智能客服下载oss服务上的json文件开始----------------结束");

        log.info("--------人工客服下载oss服务上的json文件开始---------");
        try {
            String creditPath =""; //customerService.getEsbInterface("2");
            String fileName = creditPath.substring(creditPath.lastIndexOf("/") + 1);
            //下载文件到服务器
            fileDownloadUtil.downLoadLocal(creditPath, scanningCleaningConfig.getCreditPeopleDownloadJsonPath(), fileName);
            localPath=scanningCleaningConfig.getCreditPeopleDownloadJsonPath() + fileName;
            intelligenceCallService.creditIntelligenceCall(localPath, feedBackConfigMap,
                    ckCreditLogs, noDia, stopCkCreditStopNoticePeoples,"人工客服",noCall);
        } catch (Exception e) {
            log.error("--------人工客服下载oss服务上的json文件异常---------" + e.getMessage(), e);

        }
        log.info("--------人工客服下载oss服务上的json文件结束---------");
        log.info("信贷下载任务-------------------------结束");
    }


}
